package com.dianping.cat.consumer.storage.model.transform;

import com.dianping.cat.consumer.storage.model.entity.Domain;
import com.dianping.cat.consumer.storage.model.entity.Machine;
import com.dianping.cat.consumer.storage.model.entity.Operation;
import com.dianping.cat.consumer.storage.model.entity.Segment;
import com.dianping.cat.consumer.storage.model.entity.Sql;
import com.dianping.cat.consumer.storage.model.entity.StorageReport;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.xml.sax.Attributes;









public class DefaultSaxMaker
  extends Object
  implements IMaker<Attributes>
{
  public Domain buildDomain(Attributes attributes) {
/*  28 */     String id = attributes.getValue("id");
    return new Domain(id);
  }





  public String buildId(Attributes attributes) { throw new UnsupportedOperationException(); }




/*  41 */   public String buildIp(Attributes attributes) { throw new UnsupportedOperationException(); }



  public Machine buildMachine(Attributes attributes) {
/*  46 */     String id = attributes.getValue("id");
/*  47 */     return new Machine(id);
  }





/*  54 */   public String buildOp(Attributes attributes) { throw new UnsupportedOperationException(); }



  public Operation buildOperation(Attributes attributes) {
/*  59 */     String id = attributes.getValue("id");
/*  60 */     String count = attributes.getValue("count");
/*  61 */     String avg = attributes.getValue("avg");
/*  62 */     String sum = attributes.getValue("sum");
/*  63 */     String error = attributes.getValue("error");
/*  64 */     String longCount = attributes.getValue("long-count");
/*  65 */     Operation operation = new Operation(id);

/*  67 */     if (count != null) {
/*  68 */       operation.setCount(((Long)convert(Long.class, count, Long.valueOf(0L))).longValue());
    }

/*  71 */     if (avg != null) {
/*  72 */       operation.setAvg(toNumber(avg, "0.0", Integer.valueOf(0)).doubleValue());
    }

/*  75 */     if (sum != null) {
/*  76 */       operation.setSum(toNumber(sum, "0.0", Integer.valueOf(0)).doubleValue());
    }

/*  79 */     if (error != null) {
/*  80 */       operation.setError(((Long)convert(Long.class, error, Long.valueOf(0L))).longValue());
    }

/*  83 */     if (longCount != null) {
/*  84 */       operation.setLongCount(((Long)convert(Long.class, longCount, Long.valueOf(0L))).longValue());
    }

/*  87 */     return operation;
  }


  public Segment buildSegment(Attributes attributes) {
/*  92 */     String id = attributes.getValue("id");
/*  93 */     String count = attributes.getValue("count");
/*  94 */     String avg = attributes.getValue("avg");
/*  95 */     String sum = attributes.getValue("sum");
/*  96 */     String error = attributes.getValue("error");
/*  97 */     String longCount = attributes.getValue("long-count");
/*  98 */     Segment segment = new Segment((id == null) ? 0 : ((Integer)convert(Integer.class, id, Integer.valueOf(0))).intValue());

/* 100 */     if (count != null) {
/* 101 */       segment.setCount(((Long)convert(Long.class, count, Long.valueOf(0L))).longValue());
    }

/* 104 */     if (avg != null) {
/* 105 */       segment.setAvg(toNumber(avg, "0.0", Integer.valueOf(0)).doubleValue());
    }

/* 108 */     if (sum != null) {
/* 109 */       segment.setSum(toNumber(sum, "0.0", Integer.valueOf(0)).doubleValue());
    }

/* 112 */     if (error != null) {
/* 113 */       segment.setError(((Long)convert(Long.class, error, Long.valueOf(0L))).longValue());
    }

/* 116 */     if (longCount != null) {
/* 117 */       segment.setLongCount(((Long)convert(Long.class, longCount, Long.valueOf(0L))).longValue());
    }

/* 120 */     return segment;
  }


  public Sql buildSql(Attributes attributes) {
/* 125 */     String id = attributes.getValue("id");
/* 126 */     String statement = attributes.getValue("statement");
/* 127 */     String count = attributes.getValue("count");
/* 128 */     Sql sql = new Sql(id);

/* 130 */     if (statement != null) {
/* 131 */       sql.setStatement(statement);
    }

/* 134 */     if (count != null) {
/* 135 */       sql.setCount(((Integer)convert(Integer.class, count, Integer.valueOf(0))).intValue());
    }

/* 138 */     return sql;
  }


  public StorageReport buildStorageReport(Attributes attributes) {
/* 143 */     String id = attributes.getValue("id");
/* 144 */     String name = attributes.getValue("name");
/* 145 */     String type = attributes.getValue("type");
/* 146 */     String startTime = attributes.getValue("startTime");
/* 147 */     String endTime = attributes.getValue("endTime");
/* 148 */     StorageReport storageReport = new StorageReport(id);

/* 150 */     if (name != null) {
/* 151 */       storageReport.setName(name);
    }

/* 154 */     if (type != null) {
/* 155 */       storageReport.setType(type);
    }

/* 158 */     if (startTime != null) {
/* 159 */       storageReport.setStartTime(toDate(startTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/* 162 */     if (endTime != null) {
/* 163 */       storageReport.setEndTime(toDate(endTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/* 166 */     return storageReport;
  }


  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 171 */     if (value == null || value.length() == 0) {
/* 172 */       return defaultValue;
    }

/* 175 */     if (type == Boolean.class || type == boolean.class)
/* 176 */       return (T)Boolean.valueOf(value); 
/* 177 */     if (type == Integer.class || type == int.class)
/* 178 */       return (T)Integer.valueOf(value); 
/* 179 */     if (type == Long.class || type == long.class)
/* 180 */       return (T)Long.valueOf(value); 
/* 181 */     if (type == Short.class || type == short.class)
/* 182 */       return (T)Short.valueOf(value); 
/* 183 */     if (type == Float.class || type == float.class)
/* 184 */       return (T)Float.valueOf(value); 
/* 185 */     if (type == Double.class || type == double.class)
/* 186 */       return (T)Double.valueOf(value); 
/* 187 */     if (type == Byte.class || type == byte.class)
/* 188 */       return (T)Byte.valueOf(value); 
/* 189 */     if (type == Character.class || type == char.class) {
/* 190 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 192 */     return (T)value;
  }


  protected Date toDate(String str, String format, Date defaultValue) {
/* 197 */     if (str == null || str.length() == 0) {
/* 198 */       return defaultValue;
    }

    try {
/* 202 */       return (new SimpleDateFormat(format)).parse(str);
/* 203 */     } catch (ParseException e) {
/* 204 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }

  protected Number toNumber(String str, String format, Number defaultValue) {
/* 209 */     if (str == null || str.length() == 0) {
/* 210 */       return defaultValue;
    }

    try {
/* 214 */       return (new DecimalFormat(format)).parse(str);
/* 215 */     } catch (ParseException e) {
/* 216 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/storage/model/transform/DefaultSaxMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */